<script setup lang="ts">
import {onMounted, ref} from "vue";
import { useInspectionCheckHook } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Delete from "@iconify-icons/ep/delete";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import { useUserStoreHook } from "@/store/modules/user";
import InspectionCheckFormModal from "./inspectionCheck-form-modal.vue";
import InspectionCheckDetailModal from "./inspectionCheck-detail-modal.vue";
import EditPen from "@iconify-icons/ep/edit-pen";
import { InspectionCheckDTO } from "@/api/airport/inspection/inspectionCheck";
import AddFill from "@iconify-icons/ri/add-circle-line";
import {CommonUtils} from "@/utils/common";
import {useRoute} from "vue-router";
import router from "@/router";
import {ElMessage} from "element-plus";


defineOptions({
  name: "InspectionCheck"
});

const tableRef = ref();
const searchFormRef = ref();

const { hasAdminPermission } = useUserStoreHook();
// 批量删除权限控制处理函数
const handleBulkDeleteWithPermission = (tableRef) => {
  debugger
  if (hasAdminPermission) {
    handleBulkDelete(tableRef);
  } else {
    ElMessage.warning("您没有部门负责人权限");
  }
};

const {
  searchFormParams,
  pageLoading,
  columns,
  dataList,
  pagination,
  timeRange,
  defaultSort,
  multipleSelection,
  onSearch,
  resetForm,
  onSortChanged,
  exportAllExcel,
  getInspectionCheckList,
  handleDelete,
  handleBulkDelete,
  deptTree,
  inspectionResultOptions
} = useInspectionCheckHook();

const opType = ref<"add" | "update">("add");
const modalVisible = ref(false);
const detailVisible = ref(false);

const opRow = ref<InspectionCheckDTO>();
const route = useRoute();

onMounted(() => {
  const inspectionId = route.query.inspectionId;
  if (inspectionId) {
    searchFormParams.inspectionId = Number(inspectionId);
    getInspectionCheckList();
  }
});


function openDetail(row?: InspectionCheckDTO) {
  opRow.value = row;
  detailVisible.value = true;
}

function openReformMainPage(reformCode: string) {
  router.push({
    name: 'reformMain',  // 确保路由配置中存在该名称
    query: {
      code: reformCode
    }
  });
}

function openReportListPage(reportId: number) {
  router.push({
    name: 'reportList',  // 确保路由配置中存在该名称
    query: {
      reportId: reportId.toString()
    }
  });
}

// 添加重置处理函数
function handleReset(searchFormRef, tableRef) {

  debugger
  // 1. 调用原有的重置表单方法
  resetForm(searchFormRef, tableRef);

  // 2. 清除URL中的reportId参数
  if (route.query.inspectionId) {
    const { inspectionId, ...otherQuery } = route.query;
    router.replace({
      path: route.path,
      query: Object.keys(otherQuery).length ? otherQuery : undefined
    });
  }
}

</script>

<template>
  <div class="main">
    <!-- 搜索栏 -->
    <el-form
      ref="searchFormRef"
      :inline="true"
      :model="searchFormParams"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="检查结果" prop="inspectionResult">
        <el-select
          v-model="searchFormParams.inspectionResult"
          placeholder="请选择检查结果"
          clearable
          class="!w-[200px]"
        >
          <el-option
            v-for="item in inspectionResultOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查部门" prop="deptId">
        <el-cascader
          v-model="searchFormParams.dutyDeptId"
          :options="deptTree"
          :props="{
            value: 'value',
            label: 'label',
            emitPath: false,
            checkStrictly: true
          }"
          clearable
          placeholder="请选择部门"
          class="!w-[200px]"
        />
      </el-form-item>

<!--      <el-form-item label="判定标准" prop="determinationStandard">-->
<!--        <el-input-->
<!--          v-model="searchFormParams.determinationStandard"-->
<!--          placeholder="请输入判定标准(模糊查询)"-->
<!--          clearable-->
<!--          class="!w-[200px]"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="检查人" prop="inspectionUserName">
        <el-input
          v-model="searchFormParams.inspectionUserName"
          placeholder="请输入检查人"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      <el-form-item label="检查开始" prop="startTime">
        <el-date-picker
          class="!w-[240px]"
          v-model="timeRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="pageLoading"
          @click="onSearch(tableRef)"
        >
          搜索
        </el-button>
        <el-button
          :icon="useRenderIcon(Refresh)"
          @click="handleReset(searchFormRef, tableRef)"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <!-- table bar 包裹  table -->
    <PureTableBar title="检查项列表" :columns="columns" @refresh="onSearch">
      <!-- 表格操作栏 -->
      <template #buttons>
<!--        <el-button-->
<!--          type="primary"-->
<!--          :icon="useRenderIcon(AddFill)"-->
<!--          @click="openDialog('add')"-->
<!--        >-->
<!--          新增检查项-->
<!--        </el-button>-->
        <el-button
          type="danger"
          :icon="useRenderIcon(Delete)"
          @click="handleBulkDeleteWithPermission(tableRef)"
        >
          批量删除
        </el-button>
<!--        <el-button-->
<!--          type="primary"-->
<!--          @click="CommonUtils.exportExcel(columns, dataList, '检查项列表')"-->
<!--        >单页导出</el-button-->
<!--        >-->
<!--        <el-button type="primary" @click="exportAllExcel">全部导出</el-button>-->
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          border
          ref="tableRef"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="pageLoading"
          :size="size"
          adaptive
          :data="dataList"
          :columns="dynamicColumns"
          :default-sort="defaultSort"
          :pagination="pagination"
          :paginationSmall="size === 'small' ? true : false"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="getInspectionCheckList"
          @page-current-change="getInspectionCheckList"
          @sort-change="onSortChanged"
          @selection-change="
            rows => (multipleSelection = rows.map(item => item.inspectionCheckId))
          "
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDetail(row)"
            >
              详情
            </el-button>

            <!-- dealType=1 时显示查看关联整改按钮 -->
            <template v-if="row.dealType === '1'">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="openReformMainPage(row.reformCode)"
              >
                查看关联整改
              </el-button>
            </template>

            <!-- dealType=2 时显示查看关联安全信息按钮 -->
            <template v-if="row.dealType === '2'">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="openReportListPage(row.reportId)"
              >
                查看关联安全信息
              </el-button>
            </template>

<!--            <el-button-->
<!--              class="reset-margin"-->
<!--              link-->
<!--              type="primary"-->
<!--              :size="size"-->
<!--              :icon="useRenderIcon(EditPen)"-->
<!--              @click="openDialog('update', row)"-->
<!--            >-->
<!--              编辑-->
<!--            </el-button>-->
<!--            <el-popconfirm-->
<!--              :title="`是否确认删除编号为${row.inspectionCheckId}的这个检查项`"-->
<!--              @confirm="handleDelete(row)"-->
<!--            >-->
<!--              <template #reference>-->
<!--                <el-button-->
<!--                  class="reset-margin"-->
<!--                  link-->
<!--                  type="danger"-->
<!--                  :size="size"-->
<!--                  :icon="useRenderIcon(Delete)"-->
<!--                >-->
<!--                  删除-->
<!--                </el-button>-->
<!--              </template>-->
<!--            </el-popconfirm>-->
          </template>
        </pure-table>
      </template>
    </PureTableBar>

    <inspectionCheck-form-modal
      v-model="modalVisible"
      :type="opType"
      :row="opRow"
      @success="onSearch"
    />
    <inspectionCheck-detail-modal
      v-model="detailVisible"
      :row="opRow"
      @success="onSearch"
    />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
